home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / skey / src / md4.c < prev    next >
C/C++ Source or Header  |  1993-10-20  |  9KB  |  317 lines

  1. /* 
  2.  * md4.c -- Implementation of MD4 Message Digest Algorithm
  3.  * Updated: 2/16/90 by Ronald L. Rivest
  4.  * (C) 1990 RSA Data Security, Inc.
  5.  *
  6.  * Portability nits fixed and reformatted - 2/12/91 Phil Karn
  7.  */
  8.  
  9. /* 
  10.  * To use MD4:
  11.  *   -- Include md4.h in your program
  12.  *   -- Declare an MDstruct MD to hold the state of the digest computation.
  13.  *   -- Initialize MD using MDbegin(&MD)
  14.  *   -- For each full block (64 bytes) X you wish to process, call
  15.  *          MDupdate(&MD,X,512)
  16.  *      (512 is the number of bits in a full block.)
  17.  *   -- For the last block (less than 64 bytes) you wish to process,
  18.  *          MDupdate(&MD,X,n)
  19.  *      where n is the number of bits in the partial block. A partial
  20.  *      block terminates the computation, so every MD computation should
  21.  *      terminate by processing a partial block, even if it has n = 0.
  22.  *   -- The message digest is available in MD.buffer[0] ... MD.buffer[3].
  23.  *      (Least-significant byte of each word should be output first.)
  24.  *   -- You can print out the digest using MDprint(&MD)
  25.  */
  26.  
  27. /* Implementation notes:
  28.  * This implementation assumes that longs are 32-bit quantities.
  29.  * If the machine stores the least-significant byte of an long in the
  30.  * least-addressed byte (eg., VAX and 8086), then LOWBYTEFIRST should be
  31.  * set to TRUE.  Otherwise (eg., SUNS), LOWBYTEFIRST should be set to
  32.  * FALSE.  Note that on machines with LOWBYTEFIRST FALSE the routine
  33.  * MDupdate modifies has a side-effect on its input array (the order of bytes
  34.  * in each word are reversed).  If this is undesired a call to MDreverse(X) can
  35.  * reverse the bytes of X back into order after each call to MDupdate.
  36.  */
  37. #define TRUE  1
  38. #define FALSE 0
  39.  
  40. #if (defined(__MSDOS__) || defined(MPU8086) || defined(MPU8080) \
  41.  || defined(vax) || defined (MIPSEL))
  42. #define LOWBYTEFIRST TRUE    /* Low order bytes are first in memory */
  43. #else            /* Almost all other machines are big-endian */
  44. #define    LOWBYTEFIRST FALSE
  45. #endif
  46.  
  47.  
  48. /* Compile-time includes */
  49. #include <stdio.h>
  50. #include "md4.h"
  51.  
  52. /* Compile-time declarations of MD4 ``magic constants'' */
  53. #define I0  0x67452301       /* Initial values for MD buffer */
  54. #define I1  0xefcdab89
  55. #define I2  0x98badcfe
  56. #define I3  0x10325476
  57. #define C2  013240474631     /* round 2 constant = sqrt(2) in octal */
  58. #define C3  015666365641     /* round 3 constant = sqrt(3) in octal */
  59. /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  60.  * (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  61.  * Table 2, page 660.
  62.  */
  63. #define fs1  3               /* round 1 shift amounts */
  64. #define fs2  7   
  65. #define fs3 11  
  66. #define fs4 19  
  67. #define gs1  3               /* round 2 shift amounts */
  68. #define gs2  5   
  69. #define gs3  9   
  70. #define gs4 13  
  71. #define hs1  3               /* round 3 shift amounts */
  72. #define hs2  9 
  73. #define hs3 11 
  74. #define hs4 15
  75.  
  76.  
  77. /* Compile-time macro declarations for MD4.
  78.  * Note: The ``rot'' operator uses the variable ``tmp''.
  79.  * It assumes tmp is declared as unsigned long, so that the >>
  80.  * operator will shift in zeros rather than extending the sign bit.
  81.  */
  82. #define    f(X,Y,Z)             ((X&Y) | ((~X)&Z))
  83. #define    g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
  84. #define h(X,Y,Z)             (X^Y^Z)
  85. #define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
  86. #define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
  87. #define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
  88. #define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
  89.  
  90. void MDreverse __ARGS((unsigned long *X));
  91.  
  92. /* MDprint(MDp)
  93.  * Print message digest buffer MDp as 32 hexadecimal digits.
  94.  * Order is from low-order byte of buffer[0] to high-order byte of buffer[3].
  95.  * Each byte is printed with high-order hexadecimal digit first.
  96.  * This is a user-callable routine.
  97.  */
  98. void 
  99. MDprint(MDp)
  100. MDptr MDp;
  101. {
  102.     int i,j;
  103.  
  104.     for(i=0;i<4;i++)
  105.         for(j=0;j<32;j=j+8)
  106.             printf("%02lx",(MDp->buffer[i]>>j) & 0xFF);
  107. }
  108.  
  109. /* MDbegin(MDp)
  110.  * Initialize message digest buffer MDp. 
  111.  * This is a user-callable routine.
  112.  */
  113. void 
  114. MDbegin(MDp)
  115. MDptr MDp;
  116. {
  117.     int i;
  118.  
  119.     MDp->buffer[0] = I0;  
  120.     MDp->buffer[1] = I1;  
  121.     MDp->buffer[2] = I2;  
  122.     MDp->buffer[3] = I3; 
  123.     for(i=0;i<8;i++)
  124.         MDp->count[i] = 0;
  125.     MDp->done = 0;
  126. }
  127.  
  128. /* MDreverse(X)
  129.  * Reverse the byte-ordering of every long in X.
  130.  * Assumes X is an array of 16 longs.
  131.  * The macro revx reverses the byte-ordering of the next word of X.
  132.  */
  133. #define revx { t = (*X << 16) | (*X >> 16); \
  134.            *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
  135. void
  136. MDreverse(X)
  137. unsigned long *X;
  138. {
  139.     register unsigned long t;
  140.  
  141.     revx;
  142.     revx;
  143.     revx;
  144.     revx;
  145.     revx;
  146.     revx;
  147.     revx;
  148.     revx;
  149.     revx;
  150.     revx;
  151.     revx;
  152.     revx;
  153.     revx;
  154.     revx;
  155.     revx;
  156.     revx;
  157. }
  158.  
  159. /* MDblock(MDp,X)
  160.  * Update message digest buffer MDp->buffer using 16-word data block X.
  161.  * Assumes all 16 words of X are full of data.
  162.  * Does not update MDp->count.
  163.  * This routine is not user-callable. 
  164.  */
  165. static void
  166. MDblock(MDp,X)
  167. MDptr MDp;
  168. unsigned long *X;
  169.     register unsigned long tmp, A, B, C, D;
  170.  
  171. #if LOWBYTEFIRST == FALSE
  172.     MDreverse(X);
  173. #endif
  174.     A = MDp->buffer[0];
  175.     B = MDp->buffer[1];
  176.     C = MDp->buffer[2];
  177.     D = MDp->buffer[3];
  178.     /* Update the message digest buffer */
  179.     ff(A,B,C,D,0,fs1); /* Round 1 */
  180.     ff(D,A,B,C,1,fs2); 
  181.     ff(C,D,A,B,2,fs3); 
  182.     ff(B,C,D,A,3,fs4); 
  183.     ff(A,B,C,D,4,fs1); 
  184.     ff(D,A,B,C,5,fs2); 
  185.     ff(C,D,A,B,6,fs3); 
  186.     ff(B,C,D,A,7,fs4); 
  187.     ff(A,B,C,D,8,fs1); 
  188.     ff(D,A,B,C,9,fs2); 
  189.     ff(C,D,A,B,10,fs3); 
  190.     ff(B,C,D,A,11,fs4); 
  191.     ff(A,B,C,D,12,fs1); 
  192.     ff(D,A,B,C,13,fs2); 
  193.     ff(C,D,A,B,14,fs3); 
  194.     ff(B,C,D,A,15,fs4); 
  195.     gg(A,B,C,D,0,gs1); /* Round 2 */
  196.     gg(D,A,B,C,4,gs2); 
  197.     gg(C,D,A,B,8,gs3); 
  198.     gg(B,C,D,A,12,gs4); 
  199.     gg(A,B,C,D,1,gs1); 
  200.     gg(D,A,B,C,5,gs2); 
  201.     gg(C,D,A,B,9,gs3); 
  202.     gg(B,C,D,A,13,gs4); 
  203.     gg(A,B,C,D,2,gs1); 
  204.     gg(D,A,B,C,6,gs2); 
  205.     gg(C,D,A,B,10,gs3); 
  206.     gg(B,C,D,A,14,gs4); 
  207.     gg(A,B,C,D,3,gs1); 
  208.     gg(D,A,B,C,7,gs2); 
  209.     gg(C,D,A,B,11,gs3); 
  210.     gg(B,C,D,A,15,gs4);  
  211.     hh(A,B,C,D,0,hs1); /* Round 3 */
  212.     hh(D,A,B,C,8,hs2); 
  213.     hh(C,D,A,B,4,hs3); 
  214.     hh(B,C,D,A,12,hs4); 
  215.     hh(A,B,C,D,2,hs1); 
  216.     hh(D,A,B,C,10,hs2); 
  217.     hh(C,D,A,B,6,hs3); 
  218.     hh(B,C,D,A,14,hs4); 
  219.     hh(A,B,C,D,1,hs1); 
  220.     hh(D,A,B,C,9,hs2); 
  221.     hh(C,D,A,B,5,hs3); 
  222.     hh(B,C,D,A,13,hs4); 
  223.     hh(A,B,C,D,3,hs1); 
  224.     hh(D,A,B,C,11,hs2); 
  225.     hh(C,D,A,B,7,hs3); 
  226.     hh(B,C,D,A,15,hs4);
  227.     MDp->buffer[0] += A; 
  228.     MDp->buffer[1] += B;
  229.     MDp->buffer[2] += C;
  230.     MDp->buffer[3] += D; 
  231. }
  232.  
  233. /* MDupdate(MDp,X,count)
  234.  * Input: MDp -- an MDptr
  235.  *        X -- a pointer to an array of unsigned characters.
  236.  *        count -- the number of bits of X to use.
  237.  *                 (if not a multiple of 8, uses high bits of last byte.)
  238.  * Update MDp using the number of bits of X given by count.
  239.  * This is the basic input routine for an MD4 user.
  240.  * The routine completes the MD computation when count < 512, so
  241.  * every MD computation should end with one call to MDupdate with a
  242.  * count less than 512.  A call with count 0 will be ignored if the
  243.  * MD has already been terminated (done != 0), so an extra call with count
  244.  * 0 can be given as a ``courtesy close'' to force termination if desired.
  245.  */
  246. void 
  247. MDupdate(MDp,X,count)
  248. MDptr MDp;
  249. unsigned char *X;
  250. unsigned int count;
  251. {
  252.     int i,bit,byte,mask;
  253.     unsigned long tmp;
  254.     unsigned char XX[64];
  255.     unsigned char *p;
  256.  
  257.     /* return with no error if this is a courtesy close with count
  258.      * zero and MDp->done is true.
  259.      */
  260.     if(count == 0 && MDp->done)
  261.         return;
  262.     /* check to see if MD is already done and report error */
  263.     if(MDp->done){
  264.         printf("\nError: MDupdate MD already done.");
  265.         return;
  266.     }
  267.     /* Add count to MDp->count */
  268.     tmp = count;
  269.     p = MDp->count;
  270.     while(tmp){
  271.         tmp += *p;
  272.         *p++ = tmp;
  273.         tmp = tmp >> 8;
  274.     }
  275.     /* Process data */
  276.     if(count == 512){
  277.         /* Full block of data to handle */
  278.         MDblock(MDp,(unsigned long *)X);
  279.     } else if(count > 512){
  280.         /* Check for count too large */
  281.         printf("\nError: MDupdate called with illegal count value %ld.",count);
  282.         return;
  283.     } else {
  284.         /* partial block -- must be last block so finish up
  285.          * Find out how many bytes and residual bits there are
  286.          */
  287.         byte = count >> 3;
  288.         bit =  count & 7;
  289.         /* Copy X into XX since we need to modify it */
  290.         for(i=0;i<=byte;i++)
  291.             XX[i] = X[i];
  292.         for(i=byte+1;i<64;i++)
  293.             XX[i] = 0;
  294.         /* Add padding '1' bit and low-order zeros in last byte */
  295.         mask = 1 << (7 - bit);
  296.         XX[byte] = (XX[byte] | mask) & ~( mask - 1);
  297.         /* If room for bit count, finish up with this block */
  298.         if(byte <= 55){
  299.             for(i=0;i<8;i++)
  300.                 XX[56+i] = MDp->count[i];
  301.             MDblock(MDp,(unsigned long *)XX);
  302.         } else {
  303.             /* need to do two blocks to finish up */
  304.             MDblock(MDp,(unsigned long *)XX);
  305.             for(i=0;i<56;i++)
  306.                 XX[i] = 0;
  307.             for(i=0;i<8;i++)
  308.                 XX[56+i] = MDp->count[i];
  309.             MDblock(MDp,(unsigned long *)XX);
  310.         }
  311.     /* Set flag saying we're done with MD computation */
  312.     MDp->done = 1;
  313.     }
  314. }
  315. /* End of md4.c */
  316.